<?php
class Gallery_management extends MY_Controller{
	
 	function __construct()
    {
    	parent::__construct();
        /*$this->load->library('session');*/
    }
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
	function index($folder_id = 0){
		$this->load->model('Language');
		$langList = array();
		foreach($this->Language->get_all() as $row){
			$langList[$row['language_code']] = $row['language_name'];
		}
		$this->load->model("Folder");
		$list_folder = $this->Folder->get_all();
		$callback = $this->input->get('callback',null); 
		if(empty($folder_id)&&empty($callback))
    		$this->_setLayout('admin/gallery_management','admin_layout',array('langList'=>$langList,'session_id'=>$this->session->userdata('session_id'),'list_folder'=>$list_folder,'folder_id'=>$folder_id));
    	else 
    		$this->load->view('admin/gallery_management',array('langList'=>$langList,'session_id'=>$this->session->userdata('session_id'),'list_folder'=>$list_folder,'folder_id'=>$folder_id,'callback'=>$callback));
	}
	
	function delete_picture(){
		try{
			$id =$this->input->post('id');			
    		$this->load->model('Picture');
    		$this->load->model('Folder_picture');
    		$current = $this->Picture->get_by_id($id);
			if(!empty($current['url'])&&file_exists(str_replace('//','/',getcwd().'/public/upload/') . $current['url'])){
				unlink(str_replace('//','/',getcwd().'/public/upload/') . $current['url']);
				unlink(str_replace('//','/',getcwd().'/public/upload/fix/') . $current['url']);
			}
			$this->Picture->delete($id);
			$this->Folder_picture->delete_by_field(array('picture_id'=>$id));    		
			echo json_encode(array('success'=>true));
		}
    	catch (Exception $e){
    		echo json_encode(array('success'=>false));
    	}
    }
	function picture_list($folder_id=0)
		{	
				$this->load->model('Picture');
				$start = ($this->input->get('per_page'))?$this->input->get('per_page'):0;
				$folder_to_add = $this->input->get('folder_id',0);
				$this->load->model('Folder_picture');
				$pictures_of_folder = array();
				foreach($this->Folder_picture->get_by_field(array('folder_id'=>$folder_to_add)) as $value){
					$pictures_of_folder[] = $value['picture_id'];
				}
				$this->load->library('pagination');
				
				$config['base_url'] = base_url().'admin/gallery_management/picture_list.html';
				$config['total_rows'] = $this->Picture->get_total($folder_id);
				$limit = $config['per_page'] = '8';
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$data = array('pagination'=>$this->pagination->create_links_ajax($folder_id),
							  'picture_list'=>$this->Picture->get_list($folder_id,$start,$limit),
							  'start'=>$start,
							  'pictures_of_folder'=>$pictures_of_folder,
							  'folder_id'=>$folder_id
							 );
				$this->load->view('admin/picture_list',$data);
		}
    function uploadify() {
	    if (!empty($_FILES)) {
	    	$this->load->library('helpfunc');
			
			$file_input_tmp = $_FILES['Filedata']['tmp_name'];
			$file_input_name = $_FILES['Filedata']['name'];
			$dir_upload = str_replace('//','/',getcwd() . $_REQUEST['folder']);
			$dir_fix = str_replace('//','/',$dir_upload.'/fix/');
			$prefix = 'picture';
			$file_name = $this->helpfunc->copy_and_change_filename($file_input_tmp, $file_input_name,$dir_upload, $prefix);
			$this->helpfunc->copy_and_resize($file_name,$dir_upload,$dir_fix,200,150);			
			echo $file_name;		
		}
    }
	function upload_picture() {
		$id = $this->input->get('id');
	    if ($id&&!empty($_FILES)) {
	    	$this->load->library('helpfunc');
			$this->load->model('Picture');	    	
			$file_input_tmp = $_FILES['Filedata']['tmp_name'];
			$file_input_name = $_FILES['Filedata']['name'];
			$dir_upload = str_replace('//','/',getcwd() . '/public/upload/');
			$dir_fix = str_replace('//','/',$dir_upload.'/fix/');
			$prefix = 'picture';
			$file_name = $this->helpfunc->copy_and_change_filename($file_input_tmp, $file_input_name,$dir_upload, $prefix);
			$this->helpfunc->copy_and_resize($file_name,$dir_upload,$dir_fix,200,150);			
	    	$current = $this->Picture->get_by_id($id);
			if(!empty($current['url'])&&file_exists(str_replace('//','/',getcwd().'/public/upload/') . $current['url'])){
				unlink(str_replace('//','/',getcwd().'/public/upload/') . $current['url']);
				unlink(str_replace('//','/',getcwd().'/public/upload/fix/') . $current['url']);
			}
			$this->Picture->update(array('url'=>$file_name),$id);
			echo json_encode(array('msg'=>$file_name,'url'=>base_url().'public/upload/'.$file_name));
		
		}
    }
	function add_picture(){
		try{
	    	$data = $_POST;    	
	    	$this->load->model('Picture');
	    	if(isset($data['id'])){
	    		if(array_key_exists('folder_id', $data)&&!empty($data['folder_id'])&&array_key_exists('order', $data)&&!empty($data['order'])){
	    			$this->load->model('Folder_picture');	
	    			$this->Folder_picture->update_order($data['order'],$data['folder_id'],$data['id']);
	    			unset($data['order']);
	    		}
	    		$this->Picture->update($data,$data['id']);
	    		echo json_encode(array('success'=>true));
	    	}	
	    	else{
	    		$data['order'] = $this->Picture->get_max();
	    		$data['order'] = (empty($data['order']))?1:($data['order']+1);
	    		$id = $this->Picture->insert($data);
	    		if(isset($data['folder_id'])&&!empty($data['folder_id'])){
	    			$this->load->model('Folder_picture');	
	    			$order =  $this->Folder_picture->get_max('order',"folder_id={$data['folder_id']}");
	    			$order = (empty($order))?1:($order+1);
	    			$this->Folder_picture->insert(array('folder_id'=>$data['folder_id'],'picture_id'=>$id,'order'=>$order));
	    		}
	    		echo json_encode(array('success'=>true));
	    	}		 
	    	
	    	   	
    	}
    	catch (Exception $e){
    		echo json_encode(array('success'=>false,'message'=>'Can not load'));
    	}
    }
    function edit_picture(){
    	$id = $this->input->get('id');
    	$this->load->model('Picture');
    	$this->load->model('Folder_picture');
    	$data = array('current_picture'=>$this->Picture->get_by_id($id),'start'=>$this->input->get('start'));
    	$data['fid'] = $this->input->get('fid',0);
    	$data['max_order'] = ($data['fid']==0)?$this->Picture->get_max():$this->Folder_picture->get_max('order',"folder_id={$data['fid']}");;
    	if($data['fid']>0){
    		$rows = $this->Folder_picture->get_by_field(array('folder_id'=>$data['fid'],'picture_id'=>$id));
    		$data['current_picture']['order']=$rows[0]['order'];
    	}
    	$this->load->model('Language');
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name'];
		}
    	$this->load->view('admin/edit_picture',$data);
    }
    function picture_to_folder(){
    	$this->load->model('Folder_picture');
    	$picture_id=$this->input->post('picture_id');
    	$folder_id=$this->input->post('folder_id');    	
    	$action=$this->input->post('action');
    	try{
	    	switch ($action){
	    		case 'add':
	    			$order =  $this->Folder_picture->get_max('order',"folder_id=$folder_id");
	    			$order = (empty($order))?1:($order+1);
	    			$this->Folder_picture->insert(array('folder_id'=>$folder_id,'picture_id'=>$picture_id,'order'=>$order));
	    			break;
	    		case 'remove':
	    			$this->Folder_picture->delete_by_field(array('folder_id'=>$folder_id,'picture_id'=>$picture_id));
	    			break;	
	    	}
	    	echo json_encode(array('success'=>true));
    	}	
   	 catch (Exception $e){
    		echo json_encode(array('success'=>false));
    	}
    }
}